package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCartMapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {
    @Autowired
    StatMapper statMapper;
    @Autowired
    DtsCategoryMapper categoryMapper;
    @Override
    public List<CategorySellAmts> findCategoryShellData() {
        List<CategorySellAmts> list= statMapper.findCategoryShellData();
        return list;
    }

    @Override
    public List<CatVo> findCat() {
        LambdaQueryWrapper<DtsCategory> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsCategory::getPid,0);
        qw.eq(DtsCategory::getDeleted,false);
        List<DtsCategory> catLv1 = categoryMapper.selectList(qw);
        List<CatVo> catVos = catLv1.stream().map(cat -> {
            CatVo catVo = new CatVo();
            catVo.setValue(cat.getId());
            catVo.setLabel(cat.getName());
            LambdaQueryWrapper<DtsCategory> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(DtsCategory::getPid, cat.getId());
            qw1.eq(DtsCategory::getDeleted, false);
            List<DtsCategory> catLv2 = categoryMapper.selectList(qw1);
            List<CatVo> children = catLv2.stream().map(cat2 -> {
                CatVo catVo1 = new CatVo();
                catVo1.setValue(cat2.getId());
                catVo1.setLabel(cat2.getName());
                return catVo1;
            }).collect(Collectors.toList());

            catVo.setChildren(children);

            return catVo;
        }).collect(Collectors.toList());
        return catVos;
    }

    @Override
    public IPage<DtsCategory> findByPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsCategory> iPage = new Page<>(page,limit);
        QueryWrapper<DtsCategory> qw = new QueryWrapper<>();
        if(id!=null){
            qw.eq("id",id);
        }
        if(!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else{
                qw.orderByAsc(sort);
            }
        }
        categoryMapper.selectPage(iPage,qw);

        return iPage;
    }

    @Override
    public List<CatVo> findLv1() {
        LambdaQueryWrapper<DtsCategory> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsCategory::getPid,0);
        List<DtsCategory> dtsCategories = categoryMapper.selectList(qw);
        List<CatVo> catVoList = dtsCategories.stream().map(dtsCategory -> {
            CatVo catVo = new CatVo();
            catVo.setValue(dtsCategory.getId());
            catVo.setLabel(dtsCategory.getName());
            return catVo;
        }).collect(Collectors.toList());
        return catVoList;
    }

    @Override
    public DtsCategory add(DtsCategory category) {
        category.setAddTime(new Date());
        category.setDeleted(false);
        category.setUpdateTime(new Date());
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public void update(DtsCategory category) {
        category.setUpdateTime(new Date());
        category.setDeleted(false);
        category.setAddTime(new Date());
        categoryMapper.updateById(category);
    }

    @Override
    public void delete(DtsCategory category) {
        categoryMapper.deleteById(category);

    }
}
